package day_2022_7_to_8.day48;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author haomin
 * @date 2022/08/13 17:21
 **/
public class Day28_1_Num_MaxChunksToSorted {
    class Solution {
        public int maxChunksToSorted(int[] arr) {
            int n = arr.length;
            Deque<Integer> stack = new LinkedList<>();
            for (int i = 0; i < n; i++) {
                int reserve = arr[i];
                while (!stack.isEmpty() && stack.peek() > arr[i])
                    reserve = Math.max(stack.pop(), reserve);
                stack.push(reserve);
            }
            return stack.size();
        }
    }
}